﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Data;
using System.Data.SqlClient;
using System.Collections;

namespace Seminarska
{
    /// <summary>
    /// Interaction logic for Menadzer.xaml
    /// </summary>
    public partial class Menadzer : Window
    {
        private Najava najava;
        private DataTable products;
        private DataTable vraboteni;
        private bool fok = false;
        private static string connectionString = Seminarska.Properties.Settings.Default.Products;
        
        public Menadzer()
        {
            InitializeComponent();
            products = DB.GetProducts();
            vraboteni = DB.GetVraboteni();
            lstProdukti.ItemsSource = products.DefaultView;
            lstVraboteni.ItemsSource = vraboteni.DefaultView;



        }
       
        private void Window_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            txtIme.Text = " ";
            txtMatBr.Text = " ";
            txtMobBr.Text = " ";
            txtGrad.Text = " ";
            txtPlata.Text = " ";
            txtPrezime.Text = " ";
            txtAdresa.Text = " ";
            cmbPozicija.SelectedIndex = -1;
            lstProdukti.SelectedIndex = -1;
            lstVraboteni.SelectedIndex = -1;
        }

        private void logOff_Click(object sender, RoutedEventArgs e)
        {
            najava = new Najava();
            najava.Show();
            this.Close();
        }

        private void exit_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

       
        //Dodavanje na produkt
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            if (txtKod.Text != "" && txtImeProdukt.Text != "" && txtProizvoditel.Text != "" && txtCena.Text != "")
            {
                string kod = txtKod.Text;
                string imep = txtImeProdukt.Text;
                string proiz = txtProizvoditel.Text;
                double cena = Convert.ToDouble(txtCena.Text);


                bool flag = false;
             

                for(int i=0;i<lstProdukti.Items.Count;i++)
                {
                    DataRowView drw = (DataRowView)lstProdukti.Items[i];
                    if (drw.Row.ItemArray[0].ToString() == kod)
                        flag = true;
                }

                if (flag == true)
                {
                    MessageBox.Show("Postoi takov kod!!!");
                    txtKod.Text = "";
                    txtImeProdukt.Text = "";
                    txtProizvoditel.Text = "";
                    txtCena.Text = "";
                    
                }

                else if (flag == false)
                {
                    DB.dodadiProdukt(kod, imep, proiz, cena);
                    products = DB.GetProducts();
                    lstProdukti.ItemsSource = products.DefaultView;

                }

            }
            else
                MessageBox.Show("Внесете име на производот,производител,цена и негов соодветен код!","Информација");

        }


        //Brisenje na produkt
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (lstProdukti.SelectedIndex != -1)
            {
                SqlConnection konekcija = new SqlConnection();
                konekcija.ConnectionString = Seminarska.Properties.Settings.Default.Products;

                SqlCommand komanda = new SqlCommand();

                komanda.Connection = konekcija;

                DataRowView dr = (DataRowView)lstProdukti.SelectedItem;
                string kod = dr.Row.ItemArray[0].ToString();


                komanda.Parameters.AddWithValue("@kod", kod);
                komanda.CommandText = "DELETE FROM Produkti WHERE Kod=@kod";


                try
                {
                    konekcija.Open();
                    komanda.ExecuteNonQuery();
                }
                catch (Exception err)
                {

                }
                finally
                {

                    konekcija.Close();
                    products = DB.GetProducts();
                    lstProdukti.ItemsSource = products.DefaultView;
                }

            }
            else
            {
                BrisiPlus nov = new BrisiPlus();
               
                    nov.Show();
                    fok = true;
                   
            
            }
        }

        private void lstProdukti_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            DataRowView dr = (DataRowView)lstProdukti.SelectedItem;
            if (dr != null)
            {
                string kod = dr.Row.ItemArray[0].ToString();
                string imeProizvod = dr.Row.ItemArray[1].ToString();
                string imeProizvoditel = dr.Row.ItemArray[2].ToString();
                string Cena = dr.Row.ItemArray[3].ToString();

                txtKod.Text = kod;
                txtImeProdukt.Text = imeProizvod;
                txtProizvoditel.Text = imeProizvoditel;
                txtCena.Text = Cena;
            }
            
        }


        public void updateLista()
        {
            products = DB.GetProducts();
            lstProdukti.ItemsSource = products.DefaultView;
        }

        private void Window_GotFocus(object sender, RoutedEventArgs e)
        {
            if (fok == true)
            {
                products = DB.GetProducts();
                lstProdukti.ItemsSource = products.DefaultView;
                fok = false;
            }
           
        }


        //Бришење на вработен од листа
        private void btnBrisiVrab_Click(object sender, RoutedEventArgs e)
        {


            if (lstProdukti.SelectedIndex != -1)
            {
                SqlConnection konekcija = new SqlConnection();
                konekcija.ConnectionString = Seminarska.Properties.Settings.Default.Products;

                SqlCommand komanda = new SqlCommand();

                komanda.Connection = konekcija;

                DataRowView dr = (DataRowView)lstVraboteni.SelectedItem;
                string embg = dr.Row.ItemArray[0].ToString();


                komanda.Parameters.AddWithValue("@embg", embg);
                komanda.CommandText = "DELETE FROM Vraboteni WHERE EMBG=@embg";


                try
                {
                    konekcija.Open();
                    komanda.ExecuteNonQuery();
                }
                catch (Exception err)
                {

                }
                finally
                {

                    konekcija.Close();
                    vraboteni = DB.GetVraboteni();
                    lstVraboteni.ItemsSource = vraboteni.DefaultView;
                }

            }
            else
                MessageBox.Show("Не сте селектирале вработен од листата на вработени.");


        }

        private void lstVraboteni_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            DataRowView dr = (DataRowView)lstVraboteni.SelectedItem;
            if (dr != null)
            {
                string embg = dr.Row.ItemArray[0].ToString();
                string imev = dr.Row.ItemArray[1].ToString();
                string prezv = dr.Row.ItemArray[2].ToString();
                string mobilen = dr.Row.ItemArray[3].ToString();
                string adresa = dr.Row.ItemArray[4].ToString();
                string grad = dr.Row.ItemArray[5].ToString();
                string pozicija= dr.Row.ItemArray[6].ToString();
                string plata = dr.Row.ItemArray[7].ToString();

                txtIme.Text = imev;
                txtMatBr.Text = embg;
                txtPrezime.Text = prezv;
                txtMobBr.Text = mobilen;
                txtAdresa.Text = adresa;
                txtGrad.Text = grad;
                cmbPozicija.SelectedIndex = (pozicija == "Касиер" ? 2 : 1);
                txtPlata.Text = plata;
            }
        }
        //Dodavanje na vraboten
        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            if (txtMatBr.Text != "" && txtIme.Text != "" && txtPrezime.Text != "" && txtMobBr.Text != "" && txtAdresa.Text != "" && txtGrad.Text != "" && cmbPozicija.SelectedIndex!=-1 && txtPlata.Text != "")
            {
                string embg = txtMatBr.Text;
                string mobilen = txtMobBr.Text;
                string adresa = txtAdresa.Text;
                string uloga = cmbPozicija.Text;
                string ime = txtIme.Text;
                string prezime = txtPrezime.Text;
                double plata = Convert.ToDouble(txtPlata.Text);
                string grad = txtGrad.Text;


                bool flag = false;


                for (int i = 0; i < lstVraboteni.Items.Count; i++)
                {
                    DataRowView drw = (DataRowView)lstVraboteni.Items[i];
                    if (drw.Row.ItemArray[0].ToString() == embg)
                        flag = true;
                }

                if (flag == true)
                {
                    MessageBox.Show("Вработен со внесениот матичен број веќе постои!!!");
                    txtMatBr.Text = "";
                    txtIme.Text = "";
                    txtPrezime.Text = "";
                    txtAdresa.Text = "";
                    txtPlata.Text = "";
                    txtMobBr.Text = "";
                    cmbPozicija.SelectedIndex = -1;
                    txtGrad.Text = "";


                }

                else if (flag == false)
                {
                    DB.dodadiVraboten(embg, ime, prezime, mobilen, adresa, grad, uloga, plata);
                    vraboteni = DB.GetVraboteni();
                    lstVraboteni.ItemsSource = vraboteni.DefaultView;

                }

            }
            else
                MessageBox.Show("Внесете ги сите податоци за вработениот!", "Информација");

        }

     
      

       

      

   

       

     

      

    }
}
